<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>第一个 ECharts 实例</title>
    <!-- 引入 echarts.js -->
    <script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
    <script src="/js/jquery-2.2.3.min.js"></script>
</head>
<body>
<!-- 为ECharts准备一个具备大小（宽高）的Dom -->
<div id="main" style="width: 600px;height:400px;"></div>
<div id="main2" style="width: 600px;height:400px;"></div>
</body>
<script type="text/javascript">
    $(function () {
        main();
        main2();
    })

    //基于准备好的dom，初始化echarts实例
    function main() {
        var myChart = echarts.init(document.getElementById('main'));
        myChart.clear();
        var names = [];
        var mydata = [];
        $.ajax({
            method: 'get',
            url: '/echars/show/1',
            dataType: 'json',
            success: function (datas) {
                console.log(datas)
                for (var i = 0; i < datas.length; i++) {
                    names.push(datas[i].name);
                    mydata[i] = {value: datas[i].value, name: datas[i].name};
                }
                // 指定图表的配置项和数据
                var option = {
                    title: {
                        text: '报工数量统计',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b} : {c} ({d}%)'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                        data: names
                    },
                    series: [
                        {
                            name: '',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: mydata,
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option, true);
            }
        });
    }

    //基于准备好的dom，初始化echarts实例
    function main2() {
        var myChart = echarts.init(document.getElementById('main2'));
        myChart.clear();
        var names = [];
        var success = [];
        var fail = [];
        $.ajax({
            method: 'get',
            url: '/echars/show/2',
            dataType: 'json',
            success: function (datas) {
                console.log(datas)
                for (var i = 0; i < datas.length; i++) {
                    names.push(datas[i].name);
                    success.push(datas[i].success);
                    fail.push(datas[i].fail);
                    //mydata[i] = {value: datas[i].value, name: datas[i].name};
                }
                // 指定图表的配置项和数据
                var option = {
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        }
                    },
                    legend: {},
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: names
                        }
                    ],
                    yAxis: [
                        {
                            min:0,   //最小百分比
                            max:100, //最大百分比
                            type: 'value',
                            name: '单位（%）',
                            nameGap: 35,
                            nameTextStyle: { color: '#666666' },
                            axisTick: { show: false },
                            axisLabel: {
                                show: true,
                                interval: 0, // 使x轴文字显示全
                                color: '#666666',
                                formatter: '{value}%'  //y轴数值，带百分号
                            },
                            axisLine: { show: true, lineStyle: { color: '#dddddd' } },
                            splitLine: { lineStyle: { type: 'dashed', color: '#eeeeee' } }
                        }
                    ],
                    series: [
                        {
                            name: '合格率',
                            type: 'bar',
                            stack: 'Search Engine',
                            emphasis: {
                                focus: 'series'
                            },
                            data:success,
                            label: {
                                show: true,
                                position: 'inside',
                                color:'black'
                            }
                        },
                        {
                            name: '不合格率',
                            type: 'bar',
                            stack: 'Search Engine',

                            emphasis: {
                                focus: 'series'
                            },
                            data: fail,
                            label: {
                                show: true,
                                position: 'inside',
                                color:'black'
                            },
                        }
                    ]
                };
                // 使用刚指定的配置项和数据显示图表。
                myChart.setOption(option, true);
            }
        });
    }

    /**/
</script>

</html>